package com.wx.tnsadmin.controller;


import com.wx.common.api.CommonResult;
import com.wx.common.api.ResultCode;
import com.wx.tnsadmin.model.UmsMenuModel;
import com.wx.tnsadmin.model.UmsRoleModel;
import com.wx.tnsadmin.service.UmsRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

/**
 * @Author:LLH
 * @Date:2020/8/10
 */
@RequestMapping(value = "/role")
@RestController
@Api(tags = "后台角色管理")
public class UmsRoleController {

    @Autowired
    private UmsRoleService umsRoleService;


    @PreAuthorize("hasAuthority('tns:role:findAllRole')")
    @ApiOperation(value = "后台用户角色查询", notes = "后台用户角色查询")
    @RequestMapping(value = "/findAllRole", method = RequestMethod.GET)
    public CommonResult findAllRole () {
        List<UmsRoleModel> allRole = umsRoleService.findAllRole ( );
        try {
            if (CollectionUtils.isEmpty ( allRole )) {
                return CommonResult.failed ( "用户角色查询失败!" );
            } else {
                return CommonResult.success ( allRole,"用户角色查询成功!" );
            }
        } catch (Exception e) {
            e.printStackTrace ( );
            return CommonResult.failed ( ResultCode.FAILED );
        }
    }


    @ApiOperation ( value = "后台用户角色ID查询",notes = "后台用户角色ID查询")
    @RequestMapping(value = "/findByRoleId",method = RequestMethod.GET)
    public CommonResult findByRoleId(@RequestParam(required = false) Integer id) {
        UmsRoleModel byRoleId = umsRoleService.findByRoleId ( id );
        try {
            if(byRoleId==null){
                return CommonResult.failed ( "ID用户查询失败" );
            }
            else {
                return CommonResult.success (byRoleId, "ID用户查询成功" );
            }
        } catch (Exception e) {
            e.printStackTrace ( );
            return CommonResult.failed ( ResultCode.FAILED );
        }
    }

    @PreAuthorize("hasAuthority('tns:role:saveRole')")
    @ApiOperation ( value = "后台新增用户角色",notes = "后台新增用户角色")
    @RequestMapping(value = "/saveRole",method = RequestMethod.POST)
    public CommonResult saveRole(UmsRoleModel umsRoleModel) {
        Integer integer = umsRoleService.saveRole ( umsRoleModel );
        try {
            if(integer<0){
                return CommonResult.failed ( "新增用户失败" );
            }else {
                return CommonResult.success (integer, "新增用户成功" );
            }
        } catch (Exception e) {
            e.printStackTrace ( );
            return CommonResult.failed ( ResultCode.FAILED );
        }
    }

    @PreAuthorize("hasAuthority('tns:role:updateRole')")
    @ApiOperation ( value = "后台修改用户角色",notes = "后台修改用户角色")
    @RequestMapping(value = "/updateRole",method = RequestMethod.POST)
    public CommonResult updateRole(UmsRoleModel umsRoleModel) {
        Integer integer = umsRoleService.updateRole ( umsRoleModel );
        try {
            if(integer<0){
                return CommonResult.failed ( "修改用户失败" );
            }else {
                return CommonResult.success (integer, "修改用户成功" );
            }
        } catch (Exception e) {
            e.printStackTrace ( );
            return CommonResult.failed ( ResultCode.FAILED );
        }
    }

    @PreAuthorize("hasAuthority('tns:role:deleteRoleById')")
    @ApiOperation ( value = "后台删除用户角色",notes = "后台删除用户角色")
    @RequestMapping(value = "/deleteRoleById",method = RequestMethod.DELETE)
    public CommonResult deleteAdminById(@RequestParam(required = false) Integer id){
        Integer integer = umsRoleService.deleteRoleById ( id );
        try {
            if(integer<0){
                return CommonResult.failed ( "删除用户失败" );
            }else {
                return CommonResult.success (integer, "删除用户成功" );
            }
        } catch (Exception e) {
            e.printStackTrace ( );
            return CommonResult.failed ( ResultCode.FAILED );
        }
    }



    @ApiOperation ( value = "角色菜单",notes = "角色菜单")
    @RequestMapping(value = "/findMenuByRoleId",method = RequestMethod.GET)
    public CommonResult findMenuByRoleId(@RequestParam(required = false) Integer id){
        List<UmsMenuModel> menuByRoleId = umsRoleService.findMenuByRoleId ( id );
        try {
            if(menuByRoleId==null){
                return CommonResult.failed ( "查询角色菜单失败" );
            }else {
                return CommonResult.success (menuByRoleId, "查询角色菜单成功" );
            }
        } catch (Exception e) {
            e.printStackTrace ( );
            return CommonResult.failed ( ResultCode.FAILED );
        }
    }




}
